package com.ruoyi.material.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.bill.domain.ZnDepotItem;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.material.mapper.ZnMaterialInitialStockMapper;
import com.ruoyi.material.domain.ZnMaterialInitialStock;
import com.ruoyi.material.service.IZnMaterialInitialStockService;

/**
 * 产品初始库存Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-05
 */
@Service
public class ZnMaterialInitialStockServiceImpl implements IZnMaterialInitialStockService 
{
    @Autowired
    private ZnMaterialInitialStockMapper znMaterialInitialStockMapper;

    /**
     * 查询产品初始库存
     * 
     * @param id 产品初始库存主键
     * @return 产品初始库存
     */
    @Override
    public ZnMaterialInitialStock selectZnMaterialInitialStockById(Long id)
    {
        return znMaterialInitialStockMapper.selectZnMaterialInitialStockById(id);
    }

    /**
     * 查询产品初始库存列表
     * 
     * @param znMaterialInitialStock 产品初始库存
     * @return 产品初始库存
     */
    @Override
    public List<ZnMaterialInitialStock> selectZnMaterialInitialStockList(ZnMaterialInitialStock znMaterialInitialStock)
    {
        return znMaterialInitialStockMapper.selectZnMaterialInitialStockList(znMaterialInitialStock);
    }

    /**
     * 新增产品初始库存
     * 
     * @param znMaterialInitialStock 产品初始库存
     * @return 结果
     */
    @Override
    public int insertZnMaterialInitialStock(ZnMaterialInitialStock znMaterialInitialStock)
    {
        return znMaterialInitialStockMapper.insertZnMaterialInitialStock(znMaterialInitialStock);
    }

    /**
     * 修改产品初始库存
     * 
     * @param znMaterialInitialStock 产品初始库存
     * @return 结果
     */
    @Override
    public int updateZnMaterialInitialStock(ZnMaterialInitialStock znMaterialInitialStock)
    {
        return znMaterialInitialStockMapper.updateZnMaterialInitialStock(znMaterialInitialStock);
    }

    /**
     * 批量删除产品初始库存
     * 
     * @param ids 需要删除的产品初始库存主键
     * @return 结果
     */
    @Override
    public int deleteZnMaterialInitialStockByIds(Long[] ids)
    {
        return znMaterialInitialStockMapper.deleteZnMaterialInitialStockByIds(ids);
    }

    /**
     * 删除产品初始库存信息
     * 
     * @param id 产品初始库存主键
     * @return 结果
     */
    @Override
    public int deleteZnMaterialInitialStockById(Long id)
    {
        return znMaterialInitialStockMapper.deleteZnMaterialInitialStockById(id);
    }
    /**
     * 根据商品获取初始库存-多仓库
     * @param depotList
     * @param materialId
     * @return
     */
    @Override
    public BigDecimal getInitStockByMidAndDepotList(List<Long> depotList, Long materialId) {
        BigDecimal stock = BigDecimal.ZERO;

        ZnMaterialInitialStock znMaterialInitialStock=null;
    /*    ZnMaterialInitialStock znMaterialInitialStockQuery=new ZnMaterialInitialStock();
        znMaterialInitialStockQuery.setMaterialId(materialId);
        znMaterialInitialStockQuery.setDeleteFlag("0");
        znMaterialInitialStockQuery.setTenantId(SecurityUtils.getLoginUser().getUser().getTenantId());*/
        List<ZnMaterialInitialStock>    zntocks =  this.selectZnMaterialInitialStockListByDepotId(materialId,SecurityUtils.getLoginUser().getUser().getTenantId(),depotList);

       /* if(depotList!=null && depotList.size()>0) {

        } else {
            zntocks = this.selectZnMaterialInitialStockList(znMaterialInitialStockQuery);
        }*/
        if(zntocks!=null && zntocks.size()>0) {
            for(ZnMaterialInitialStock ms: zntocks) {
                if(ms!=null) {
                    stock = stock.add(ms.getNumber()!=null?ms.getNumber():BigDecimal.ZERO);
                }
            }
        }
        return stock;
    }

    @Override
    public List<ZnMaterialInitialStock> selectZnMaterialInitialStockListByDepotId(Long materialId, Long tenantId, List<Long> depotList) {
        return znMaterialInitialStockMapper.selectZnMaterialInitialStockListByDepotId(materialId,tenantId,depotList);
    }
    
}
